package ar.org.cfired.creditos.schedulers;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.jbpm.api.Configuration;
import org.jbpm.api.JobQuery;
import java.util.List;
import java.util.Date;
import org.jbpm.api.ManagementService;
import ar.org.cfired.hibernate.utils.HibernateUtil;

public class JobExecutor implements Job {
	 public void execute(JobExecutionContext arg0) throws JobExecutionException{
		 Configuration cfg = new Configuration();
		 cfg.setHibernateSessionFactory(HibernateUtil.getSessionFactory());
		 //ManagementService m = new Configuration().buildProcessEngine().getManagementService(); 
		 ManagementService m = cfg.buildProcessEngine().getManagementService();
		 JobQuery j = m.createJobQuery();
		 org.jbpm.api.job.Job job;
		 List<org.jbpm.api.job.Job> l = j.list();
		 Date fechaActual = new Date();
		 for (int i =0; i<l.size(); i++) {
			 job = l.get(i);
			 if (job.getDuedate().getTime() <= fechaActual.getTime())
				 m.executeJob(job.getId());
		 }
		 l.clear();
		 job = null;
		 fechaActual = null;
		 j = null;
		 m = null;
		 cfg = null;
	 }
	 
}
